package com.blog.utils.filter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class TxtUtil {
	public static BufferedReader bufread;

	private static String path = "D://sunclity.txt";
	private static File filename = new File(path);
	private static String readStr = "D://sunclity.txt";

	/**
	 * 创建文本文件
	 * 
	 * @throws IOException
	 */
	public static void createTexFile() throws IOException {
		if (!filename.exists()) {
			filename.createNewFile();
			System.out.println(filename + "已创建！");
		}
	}

	public static StringBuffer read() {
		StringBuffer buffer = new StringBuffer();

		return buffer;
	}

	/**
	 * 读取文本文件
	 * 
	 * @return
	 */
	public static String readTxtFile() {
		String read;
		FileReader fileread;
		try {
			fileread = new FileReader(filename);
			bufread = new BufferedReader(fileread);
			while ((read = bufread.readLine()) != null) {
				readStr = readStr + read + "\r\n";
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("文件内容是：" + readStr);
		return readStr;
	}

	/**
	 * 写文件
	 * 
	 * @param newStr
	 */
	public static void writeTxtFile(String newStr) {
		// 先读取原有文件内容，然后进行写入操作
		String filein = newStr + "\r\n" + readStr + "\r\n";
		RandomAccessFile mm = null;
		try {
			mm = new RandomAccessFile(filename, "rw");
			mm.writeBytes(filein);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (mm != null) {
				try {
					mm.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 将文件中指定内容的第一行替换为其他内容
	 * 
	 * @param oldStr
	 * @param replaceStr
	 */
	public static void replaceTxtByStr(String oldStr, String replaceStr) {
		String temp = "";

		try {
			File file = new File(path);
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();

			// 保存该行内容前面的内容
			while ((temp = br.readLine()) != null && !temp.equals(oldStr)) {
				buf = buf.append(temp);
				buf = buf.append(System.getProperty("line.sepatator"));
			}

			// 将内容插入
			buf = buf.append(replaceStr);
			// 保存该行后面的内容
			while ((temp = br.readLine()) != null) {
				buf = buf.append(System.getProperty("line.sepatator"));
				buf = buf.append(temp);
			}

			br.close();
			FileOutputStream fos = new FileOutputStream(file);
			PrintWriter pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// public static void main(String[] args) {
	// try {
	// TxtUtil.createTexFile();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// TxtUtil.readTxtFile();
	// TxtUtil.writeTxtFile(DateUtil.formatData("yyyy-MM-dd HH:mm:ss"));
	//
	// TxtUtil.replaceTxtByStr("sunclity", "\"replaceStr");
	// }

	public void replace(PrintWriter printWriter, String regex,
			String replacement) {

	}

	// public static void main(String[] argv) {
	// System.out.println(new Date().getTime());
	// try {
	// File file = new File("D:/read_test-新.txt");
	// BufferedReader bufReader = new BufferedReader(
	// new InputStreamReader(new FileInputStream(file)));
	//
	// StringBuffer strBuf = new StringBuffer();
	// for (String tmp = null; (tmp = bufReader.readLine()) != null; tmp = null)
	// {
	// // 在这里做替换操作
	// tmp = tmp.replaceAll("#", "\\*");
	// tmp = tmp.replaceFirst("===", "\\* ");
	// tmp = tmp.replaceAll("===", "");
	// tmp = tmp.replaceAll("==", "===");
	// strBuf.append(tmp);
	// strBuf.append(System.getProperty("line.separator")); // 行与行分隔符类似“\n”
	// }
	// bufReader.close();
	//
	// PrintWriter printWriter = new PrintWriter(file);
	// printWriter.write(strBuf.toString().toCharArray());
	// printWriter.flush();
	// printWriter.close();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// System.out.print(new Date().getTime());
	// }

	public static void main(String[] args) {
		readInfo();
	}

	public static void readInfo() {
		try {
			File file = new File("D:/user.txt");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(new FileInputStream(file)));

			List<User> list = new ArrayList<User>();
			String tmp = null;
			while ((tmp = bufferedReader.readLine()) != null) {
				String[] strUser = tmp.split(" ");
				if (strUser.length >= 2) {
					User user = new User();
					user.setName(strUser[0]);
					user.setPhone(strUser[1]);
					list.add(user);
				}
			}
			bufferedReader.close();

			System.out.println(net.sf.json.JSONArray.fromObject(list));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
